import React,{useState} from 'react'
import { useHistory } from 'react-router'
import './login.css'
import title from './img/题目-1.1.png'

const Login = () => {
    const [user,setUser] = useState('')
    const [pwd,setPwd] = useState('')
    const history = new useHistory()
    const getUser = (e)=>{
        setUser(e.target.value)
    }
    const getPwd = (e)=>{
        setPwd(e.target.value)
    }
    const verify = ()=>{
        fetch('https://api.52h5.xyz:3001/verify',{
            method:'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body:JSON.stringify({user:user,pwd:pwd})
        })
        // .then(res=>res.json())
        .then(res=>{
            console.log(res)
            // console.log(res.data)
            if(res.status == 200){
                localStorage.setItem("isLogin",res)
                history.push('/')
            }else{
                alert("登录失败")
            }
        }).catch(err=>{
            console.log(err)
        })
    }
    return (
        <div className="content">
            <img src={title} alt="" className='title'/>
            <div className='log'>
                <input className='first' placeholder="管理员账号" onChange={(e)=>getUser(e)}></input>
                <input className='second' placeholder="管理员密码" onChange={(e)=>getPwd(e)} type='password'></input>
                <button id="button" onClick={()=>verify()}>登录</button>
            </div>
        </div>
    )
}

export default Login
